import { marks } from './mark';
import { IFunction } from '../types/normal';
import { IProp, IMethod, IEventTurnBack } from '../types/vue-sfc';

/**
 * 拼接函数注释
 */
export function splicCommentForFunction(config: IFunction, option?: { spaceLen?: number }) {
    const spaceLen = option?.spaceLen || 0;
    const spaceChars = Array(spaceLen).fill(' ').join('');

    let comment = '';
    comment += '/**\n';
    if (config.desp) {
        comment += `${spaceChars} * ${config.desp}\n`;
    }
    if (config.params) {
        config.params.forEach(param => {
            let paramText = marks.param;
            if (param.type && param.type.length) {
                if (param.type[0] === 'any') {
                    paramText += ' {*}';
                } else {
                    paramText += ` {${param.type.join(' | ')}}`;
                }
            }
            if (param.name) {
                paramText += ` ${param.name}`;
            }
            if (param.desp) {
                paramText += ` ${param.desp}`;
            }
            comment += `${spaceChars} * ${paramText}\n`;
        });
    }
    if (config.returns) {
        let returnsText = marks.return;
        if (config.returns.type && config.returns.type.length) {
            if (config.returns.type[0] === 'any') {
                returnsText += ' {*}';
            } else {
                returnsText += ` {${config.returns.type.join(' | ')}}`;
            }
        }
        if (config.returns.desp) {
            returnsText += ` ${config.returns.desp}`;
        }
        comment += `${spaceChars} * ${returnsText}\n`;
    }

    comment += `${spaceChars} */`;
    return comment;
}

/**
 * 拼接 Vue Prop 注释
 */
export function splicCommentForVueProp(config: IProp, option?: { spaceLen?: number }) {
    const spaceLen = option?.spaceLen || 0;
    const spaceChars = Array(spaceLen).fill(' ').join('');

    let comment = '';
    comment += '/**\n';

    if (config.desp) {
        comment += `${spaceChars} * ${config.desp}\n`;
    }
    if (config.options) {
        config.options.forEach(option => {
            let text = marks.option;
            if (option.name) {
                text += ` ${option.name}`;
            }
            if (option.desp) {
                text += ` ${option.desp}`;
            }
            comment += `${spaceChars} * ${text}\n`;
        });
    }

    comment += `${spaceChars} */`;
    return comment;
}

/**
 * 拼接 Vue Method 注释
 */
export function splicCommentForVueMethod(config: IMethod, option?: { spaceLen?: number }) {
    const spaceLen = option?.spaceLen || 0;
    const spaceChars = Array(spaceLen).fill(' ').join('');

    let comment = '';
    comment += '/**\n';

    if (config.desp) {
        comment += `${spaceChars} * ${marks.public} ${config.desp}\n`;
    }
    if (config.params) {
        config.params.forEach(param => {
            let paramText = marks.param;
            if (param.type && param.type.length) {
                if (param.type[0] === 'any') {
                    paramText += ' {*}';
                } else {
                    paramText += ` {${param.type.join(' | ')}}`;
                }
            }
            if (param.name) {
                paramText += ` ${param.name}`;
            }
            if (param.desp) {
                paramText += ` ${param.desp}`;
            }
            comment += `${spaceChars} * ${paramText}\n`;
        });
    }
    if (config.returns) {
        let returnsText = marks.return;
        if (config.returns.type && config.returns.type.length) {
            if (config.returns.type[0] === 'any') {
                returnsText += ' {*}';
            } else {
                returnsText += ` {${config.returns.type.join(' | ')}}`;
            }
        }
        if (config.returns.desp) {
            returnsText += ` ${config.returns.desp}`;
        }
        comment += `${spaceChars} * ${returnsText}\n`;
    }

    comment += `${spaceChars} */`;
    return comment;
}

/**
 * 拼接 Vue Event 注释
 */
export function splicCommentForVueEvent(config: IEventTurnBack, option?: { spaceLen?: number }) {
    const spaceLen = option?.spaceLen || 0;
    const spaceChars = Array(spaceLen).fill(' ').join('');

    let comment = '';
    comment += '/**\n';

    if (config.desp) {
        comment += `${spaceChars} * ${config.desp}\n`;
    }
    if (config.params) {
        config.params.forEach(param => {
            let paramText = marks.arg;
            if (param.type && param.type.length) {
                if (param.type[0] === 'any') {
                    paramText += ' {*}';
                } else {
                    paramText += ` {${param.type.join(' | ')}}`;
                }
            }
            if (param.desp) {
                paramText += ` ${param.desp}`;
            }
            comment += `${spaceChars} * ${paramText}\n`;
        });
    }

    comment += `${spaceChars} */`;
    return comment;
}

/**
 * 拼接 Vue Event 注释
 */
export function splicCommentForVueEventDetails(eventName: string, config: IEventTurnBack) {
    let comment = `${marks.event} ${eventName}\n`;
    if (config.desp) {
        comment += ` * ${marks.description} ${config.desp}\n`;
    }
    if (config.params) {
        config.params.forEach(param => {
            let paramText = marks.arg;
            if (param.type && param.type.length) {
                if (param.type[0] === 'any') {
                    paramText += ' {*}';
                } else {
                    paramText += ` {${param.type.join(' | ')}}`;
                }
            }
            if (param.desp) {
                paramText += ` ${param.desp}`;
            }
            comment += ` * ${paramText}`;
        });
    }

    return comment;
}
